﻿using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Identity;
using WeChat.CMS.AuditLogging;

namespace WeChat.CMS.SecurityLogs
{
    public  class SecurityLogAppService:CMSAppService, ISecurityLogAppService
    {
        private readonly IIdentitySecurityLogRepository _securityLogRepository;

        public SecurityLogAppService(IIdentitySecurityLogRepository securityLogRepository)
        {
            _securityLogRepository = securityLogRepository;
 
        }
        [Authorize(AuditLogPermissions.AuditLogs.Default)]
        public virtual async Task<PagedResultDto<SecurityLogDto>> GetListAsync(GetSecurityLogDto input)
        {
            var count = await _securityLogRepository.GetCountAsync(
                startTime: input.StartTime,
                endTime: input.EndTime,
                applicationName: input.ApplicationName,
                identity: input.Identity,
                action: input.ActionName,
                userId: null,
                userName: input.UserName,
                clientId: input.ClientId,
                correlationId: input.CorrelationId
            );
            var list = await _securityLogRepository.GetListAsync(
                sorting: input.Sorting,
                maxResultCount: input.MaxResultCount,
                skipCount: input.SkipCount,
                startTime: input.StartTime,
                endTime: input.EndTime,
                applicationName: input.ApplicationName,
                identity: input.Identity,
                action: input.ActionName,
                userId: null,
                userName: input.UserName,
                clientId: input.ClientId,
                correlationId: input.CorrelationId
            );
            return new PagedResultDto<SecurityLogDto>(
                count,
                ObjectMapper.Map<List<IdentitySecurityLog>, List<SecurityLogDto>>(list)
            );
        }
    }
}
